package cn.stylefeng.guns.sys.core.util.utility;

import cn.stylefeng.guns.sys.core.util.common.SystemConstant;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import org.apache.commons.beanutils.ConvertUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.MathContext;
import java.math.RoundingMode;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 工具类
 */
public class Katrina_CJ_Utils {


    public static final String IMGPATH = "https://ahmbjy.com/images/";

    /**
     * html括号乱码转换
     *
     * @return
     */
    public static String conversionHtmlCode(String str) {
        return str.replaceAll("& #40;", "(").replaceAll("& #41;", ")");
    }


    public static Date strToDate(String str) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.parse(str);
    }

    public static String getyyyyMMdd_zh(Date date){
        //获取日历单例
        Calendar c = Calendar.getInstance();
        //格式化
        SimpleDateFormat f = new SimpleDateFormat("yyyy年MM月dd日 hh点:mm分:ss秒");
        //截取
        return f.format(c.getTime());
    }

    /**
     * List<T> 转 json 保存到数据库
     */
    public static <T> String listToJson(List<T> ts) {
        String jsons = JSON.toJSONString(ts);
        return jsons;
    }

    /**
     * json 转 List<T>
     */
    public static <T> List<T> jsonToList(String jsonString, Class<T> clazz) {
        @SuppressWarnings("unchecked")
        List<T> ts = (List<T>) JSONArray.parseArray(jsonString, clazz);
        return ts;
    }

    /**
     * 获取图片ids
     *
     * @param strArr
     * @return
     */
    public static String[] getImagesId(String strArr) {
        return strArr.split(",");
    }

    /**
     * 获取图片ids
     *
     * @param strArr
     * @return
     */
    public static Long[] getImagesId(String[] strArr) {
        System.out.println("---------->>>"+Arrays.toString((Long[]) ConvertUtils.convert(strArr,Long.class)));
        return (Long[]) ConvertUtils.convert(strArr,Long.class);
    }



    public static String getTimeStr() {
        //获取日历单例
        Calendar c = Calendar.getInstance();
        //格式化
        SimpleDateFormat f = new SimpleDateFormat("yyyyMMddHHmm");
        //截取
        return f.format(c.getTime());
    }

    /**
     * 获取不含税单价
     *
     * @param price
     * @param rate
     * @return
     */
    public static BigDecimal getPrice(BigDecimal price, Double rate) {
        //双精度，大于0.5进一位
        MathContext mc = new MathContext(2, RoundingMode.HALF_DOWN);
        BigDecimal rate_decimal = new BigDecimal(rate);
        return price.divide(rate_decimal.add(new BigDecimal(1)), mc);
    }

    /**
     * 自动生成编号
     * @param type
     * @param id
     * @return
     */
    public static String createOrderSerial(Order type, Long id) {
        return type + getTimeStr() + (id + 1);
    }


    /**
     * @param value 要转的object类型
     * @return 转成的BigDecimal类型数据
     */
    public static BigDecimal getBigDecimal(Object value) {
        BigDecimal ret = null;
        if (value != null) {
            if (value instanceof BigDecimal) {
                ret = (BigDecimal) value;
            } else if (value instanceof String) {
                ret = new BigDecimal((String) value);
            } else if (value instanceof BigInteger) {
                ret = new BigDecimal((BigInteger) value);
            } else if (value instanceof Number) {
                ret = new BigDecimal(((Number) value).doubleValue());
            } else {
                throw new ClassCastException("Not possible to coerce [" + value + "] from class " + value.getClass() + " into a BigDecimal.");
            }
        }
        return ret;
    }


    /**
     * 设置页数
     * @param page
     * @param current
     * @param size
     */
    public static  void pageSet(Page page,Long current, Long size){
        if (ToolUtil.isNotEmpty(current)){
            page.setCurrent(current);
        }
        if (ToolUtil.isNotEmpty(size)){
            page.setSize(size);
        }
    }

    /**
     * 转换字符串
     * @param ids
     * @return
     */
    public static List<Long> getIds(String ids){

        return  Arrays.asList(ids.split(",")).stream().map(str -> Long.parseLong(str)).collect(Collectors.toList());
    }


    /*public static  String areaJoin(Long areaId){
        String nameOne = addressService.getById(areaId).getName();
        String nameTwo = null;
        String nameThree  = null;
        if (addressService.getById(areaId).getPid() != 0L){
            nameTwo = addressService.getById(addressService.getById(areaId).getPid()).getName();
            if (addressService.getById(addressService.getById(areaId).getPid()).getPid() != 0L){
                nameThree = addressService.getById(addressService.getById(addressService.getById(areaId).getPid())).getName();
            }
        }
        s.setArea(JOINER.join(Lists.newArrayList(nameThree,nameTwo,nameOne)));
    }*/


}
